package io.jpress.addon.airworthiness.controller;

import com.jfinal.aop.Inject;

import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import io.jboot.db.model.Columns;
import io.jboot.web.controller.annotation.RequestMapping;

import io.jpress.addon.airworthiness.model.JpressAddonAirworthiness;
import io.jpress.addon.airworthiness.service.JpressAddonAirworthinessService;

import io.jpress.web.base.TemplateControllerBase;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RequestMapping(value = "/airworthiness", viewPath = "/")
public class JpressAddonAirworthinessController extends TemplateControllerBase {

    @Inject
    private JpressAddonAirworthinessService service;

    public void index() {

        String language = getPara("language");

        setMenuActive(menu -> {
            if (menu.isUrlEquals("/airworthiness/index")) {
                return true;
            }
            return false;
        });

        if(language==null || language.equals("ch")){
            render("airworthiness.html");
        }else if(language.equals("en")){
            render("airworthiness_en.html");
        }

    }

    //通过型号查询信息
    public void getjson(){

        String model = getPara("model");
        Columns columns = new Columns();
        columns.eq("aircraft_model", model);
        List<JpressAddonAirworthiness> listByColumns = service.paginateByColumns(1,8,columns,"effective_date desc").getList();
        renderJson(Ret.ok("airworthinessList", listByColumns));

    }

    //条件查询
    public void findData(){

        String keyword = getPara("keyword");
        String Model = getPara("model");
        String CommandNumber = getPara("commandNumber");
        String StartTime = getPara("startTime");
        String EndTime = getPara("endTime");
        StringBuffer where = new StringBuffer("where ");
        int k = 0;

        if(keyword!=null && !keyword.trim().equals("")){
            where.append("(title like '%"+keyword+"%' or title_en like '%"+keyword+"%')");
            k++;
        }
        if(Model!=null && !Model.trim().equals("")){
            where.append((k > 0 ? "and " : "") + " aircraft_model = '"+Model+"'");
            k++;
        }
        if(CommandNumber!=null && !CommandNumber.trim().equals("")){
            where.append((k > 0 ? "and " : "")+"cad_no = '"+CommandNumber+"'");
            k++;
        }
        if(StartTime!=null && !StartTime.trim().equals("")){
            where.append((k > 0 ? "and " : "")+"effective_date >= date_format('"+StartTime+"','%Y-%m-%d')");
            k++;
        }
        if(EndTime!=null && !EndTime.trim().equals("")){
            where.append((k > 0 ? "and " : "")+"effective_date <= date_format('"+EndTime+"','%Y-%m-%d')");
            k++;
        }

        String sql = "select * from jpress_addon_airworthiness " ;
        if(k>0){
            sql += where.toString();
        }

        List<Record> records = Db.find(sql);

        renderJson(Ret.ok("airworthinessList", records));

    }

    private Date strtoDate(String str){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
        Date date = null;
        try {
            date = simpleDateFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

}
